package com.zwh.algorithm.foundation;

import com.zwh.algorithm.leetcode.medium.LeetCode43;

/**
 * 链表的一些常见方法
 */
public class ListNodeFound {

    public static void main(String[] args) {
        ListNode node = new ListNode(1);
        node.next = new ListNode(2);
        node.next.next = new ListNode(3);
        node.next.next.next = new ListNode(4);
        sout(node);
        ListNode inversion = inversion(node);
        sout(inversion);
    }

    //链表输出
    public static void sout(ListNode node){
        for (ListNode node1 = node;node1 != null; node1 = node1.next){
            System.out.print(node1.val);
        }
        System.out.println();
    }
    //链表倒置
    public static ListNode inversion(ListNode node){
        ListNode node1 = null;
        if (node == null || node.next == null){
            return node;
        }
        while (node.next != null){
            ListNode next = node.next;
            node.next = node1;
            node1 = node;
            node =next;
        }
        node.next = node1;
        return node;
    }

    static class ListNode {
        int val;
        ListNode next;
        ListNode() {}
        ListNode(int val) { this.val = val; }
        ListNode(int val, ListNode next) { this.val = val; this.next = next;
        }
    }
}
